# Maintainer: Tim S <stahta01@gmail.com>
# ArchLinux Contributor: Pierre-Marie de Rodat <pmderodat on #ada at freenode.net>
# ArchLinux Contributor: Rod Kay <charlie5 on #ada at freenode.net>


_realname=gprbuild-bootstrap
pkgbase=mingw-w64-${_realname}-git
pkgname=${MINGW_PACKAGE_PREFIX}-${_realname}-git
pkgver=r3576.43e55df6
pkgrel=1

_gprbuild_commit="43e55df6c26e2bd48ab6c2f4214f497c4ddbc943"
_gprbuild_extract_dir="AdaCore-gprbuild-43e55df"
_xmlada_commit="c16f24ebf752b7d75dfdd0eaa7dd501a08a31256"
_xmlada_extract_dir="AdaCore-xmlada-c16f24e"

pkgdesc="Static GPRbuild to bootstrap XML/Ada and GPRbuild itself (mingw-w64)"
arch=('any')
url='https://github.com/AdaCore/gprbuild/'
license=('GPL3')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("git"
             "${MINGW_PACKAGE_PREFIX}-gcc-ada")
provides=("${MINGW_PACKAGE_PREFIX}-gprbuild-bootstrap")
conflicts=("${MINGW_PACKAGE_PREFIX}-gprbuild"
           "${MINGW_PACKAGE_PREFIX}-gprbuild-gpl"
           "${MINGW_PACKAGE_PREFIX}-gprbuild-bootstrap")
source=("gprbuild.tar.gz::https://api.github.com/repos/AdaCore/gprbuild/tarball/${_gprbuild_commit}"
        "xmlada.tar.gz::https://api.github.com/repos/AdaCore/xmlada/tarball/${_xmlada_commit}")
sha256sums=('d4491ae831ce3ab89ea930cc58280372325476046054cf64691bda867a52b028'
            '12139d8c3d511b516f9dcf68ee76a70f2dc52031d379e9e4942aa84346974751')

pkgver() {
  cd "${srcdir}/${_gprbuild_extract_dir}"
  printf "r%s.%s" "$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)"
}

prepare() {
  cd "${srcdir}/${_gprbuild_extract_dir}"

  # GPRbuild hard-codes references to ${MINGW_PREFIX}/libexec, but MINGW packages
  # must use ${MINGW_PREFIX}/lib instead.
  sed -i "s|libexec|lib|g" doinstall gprbuild.gpr \
    share/gprconfig/compilers.xml \
    share/gprconfig/linker.xml \
    share/gprconfig/gnat.xml
}

build() {
  cd "${srcdir}/${_gprbuild_extract_dir}"

  export GNATMAKEFLAGS="-j$(nproc)"
  export DESTDIR="${srcdir}/bootstrap"
  ./bootstrap.sh \
    --prefix=${MINGW_PREFIX} \
    --libexecdir=/lib \
    --with-xmlada="${srcdir}/${_xmlada_extract_dir}"
}

package() {
  cd "$srcdir/bootstrap"
  cp -a --no-preserve=ownership -- "${srcdir}/bootstrap/${MINGW_PREFIX}" "${pkgdir}"
}
